<?php
class TTT_Myfilter_Adminhtml_IndexController extends Mage_Adminhtml_Controller_action
{
	protected function _initAction() {
        $this->loadLayout()->_setActiveMenu('myfilter/managefilter');
        return $this;
    }

    protected function _prepareFilter()
    {
		$id = $this->_request->getParam('id', false);
		$filter = Mage::getModel('myfilter/myfilter');
		if ($id) {
			$filter->load($id);
		}
		return $filter;
    }

    public function indexAction()
    {
		$this->_initAction()->renderLayout();
    }

    /**
     * Saves the Custom Filters
     */
    public function saveajaxAction()
    {
		$entity = $this->_request->getParam('entity', false);
		$filterKey = $this->_request->getParam('filterKey', false);
		$filterValue = $this->_request->getParam('filterValue', false);
		$name = $this->_request->getParam('name', false);
		$result = array();

		if (!$name) {
			$result ['error'] = 1;
			$result ['msg'] = Mage::helper('myfilter')->__('Filter name is not set.');
		} else if (!$entity) {
			$result ['error'] = 1;
			$result ['msg'] = Mage::helper('myfilter')->__('Filter entity is not set.');
		} else if (!$filterKey) {
			$result ['error'] = 1;
			$result ['msg'] = Mage::helper('myfilter')->__('Filter parameter key is not set.');
		} else if (!$filterValue) {
			$result ['error'] = 1;
			$result ['msg'] = Mage::helper('myfilter')->__('Filter value is not set.');
		} else {
			$filter = $this->_prepareFilter();
			/*@var $filter TTT_Myfilter_Model_Myfilter*/

			$filter->setData('name', $name);
			$filter->setData('entity', $entity);
			$filter->setData('filterkey', $filterKey);
			$filter->setData('filtervalue', $this->_prepareFilterFromUrl($filterValue, $filterKey));
			$filter->setData('menu', 0);
			try {
				$filter->save();
				$result ['error'] = 0;
				$result ['msg'] = Mage::helper('myfilter')->__('Your Custom filter has been saved.');
			} catch (Exception $e) {
				$result ['error'] = 1;
				$result ['msg'] = $e->getMessage();
			}
		}

		$this->getResponse()->setBody(Zend_Json::encode($result));
    }

    private function _prepareFilterFromUrl($url, $filterKey)
    {
    	$urlArray = explode('/', $url);
    	$i = 0;
    	foreach ($urlArray as $parts) {
    		if ($parts != $filterKey) {
    			$i++;
    		} else {
    			return $urlArray [$i+1];
    		}
    	}
    }

	public function massDeleteAction() {
        $filterIds = $this->getRequest()->getParam('filter');
        if(!is_array($filterIds)) {
			Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
        } else {
            try {
                foreach ($filterIds as $filterId) {
                    $test = Mage::getModel('myfilter/myfilter')->load($filterId);
                    $test->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('adminhtml')->__(
                        'Total of %d record(s) were successfully deleted', count($filterIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }

    public function massStatusAction()
    {
        $filterIds = $this->getRequest()->getParam('filter');
        if(!is_array($filterIds)) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
        } else {
            try {
                foreach ($filterIds as $filterId) {
                	$status = $this->getRequest()->getParam('status');
					$status = ($status == 1) ? 1 : 0;
                	$test = Mage::getSingleton('myfilter/myfilter')
                        ->load($filterId)
                        ->setMenu($status)
                        ->setIsMassupdate(true)
                        ->save();
                }
                $this->_getSession()->addSuccess(
                    $this->__('Total of %d record(s) were successfully updated', count($filterIds))
                );
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }

}